namespace MauiSampleDemo.Samples.Animations;

public partial class AnimationDemo : ContentPage
{
    public AnimationDemo()
    {
        InitializeComponent();
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        await image.RotateTo(360, 2000);
        image.Rotation = 0;
    }

    private async void Button_Clicked_1(object sender, EventArgs e)
    {
        await image.RelRotateTo(360, 2000);
        image.Rotation = 0;
    }

    private async void Button_Clicked_2(object sender, EventArgs e)
    {
        await image.ScaleTo(2, 2000);
        image.Rotation = 0;
    }

    private async void Button_Clicked_3(object sender, EventArgs e)
    {
        await image.RelScaleTo(2, 2000);
        image.Rotation = 0;
    }

    private async void Button_Clicked_4(object sender, EventArgs e)
    {
        //double radius = Math.Min(40, 60) / 2;
        //image.AnchorY = radius / image.Height;
        image.AnchorX = 1.0;
        image.AnchorY = 1.0;
        await image.RotateTo(360, 2000);
        image.Rotation = 0;
    }

    private async void Button_Clicked_5(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000);
        image.Rotation = 0;
    }

    private async void Button_Clicked_6(object sender, EventArgs e)
    {
        image.Opacity = 0;
        await image.FadeTo(1, 4000);
    }

    private async void Button_Clicked_8(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.BounceIn);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_9(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.BounceOut);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_10(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.CubicIn);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_11(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.CubicInOut);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_12(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.CubicOut);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_13(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.Linear);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_14(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.SinIn);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_15(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.SinInOut);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_16(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.SinOut);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_17(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.SpringIn);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_18(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, -100, 1000, Easing.SpringOut);
        await image.TranslateTo(0, 0);
    }

    private async void Button_Clicked_19(object sender, EventArgs e)
    {
        await image.TranslateTo(-100, 0, 1000);    // Move image left
        await image.TranslateTo(-100, -100, 1000); // Move image diagonally up and left
        await image.TranslateTo(100, 100, 2000);   // Move image diagonally down and right
        await image.TranslateTo(0, 100, 1000);     // Move image left
        await image.TranslateTo(0, 0, 1000);       // Move image up
    }

    private async void Button_Clicked_20(object sender, EventArgs e)
    {
        await Task.WhenAll
                    (
                      image.TranslateTo(100, 100, 3000),
                      image.ScaleTo(0.3, 2000),
                      image.FadeTo(0, 4000)
                    );
        await image.FadeTo(1, 1000);
        await image.ScaleTo(1, 2000);
        await image.TranslateTo(0, 0, 2000);
    }

    private void Button_Clicked_21(object sender, EventArgs e)
    {
        image.CancelAnimations();
    }

    private void Button_Clicked_22(object sender, EventArgs e)
    {
        //var animation = new Animation(v => image.Scale = v, 1, 2);
        //animation.Commit(this, "SimpleAnimation", 16, 2000, Easing.Linear, (v, c) => image.Scale = 1, () => true);

        //var parentAnimation = new Animation();
        //var scaleUpAnimation = new Animation(v => image.Scale = v, 1, 2, Easing.SpringIn);
        //var rotateAnimation = new Animation(v => image.Rotation = v, 0, 360);
        //var scaleDownAnimation = new Animation(v => image.Scale = v, 2, 1, Easing.SpringOut);

        //parentAnimation.Add(0, 0.5, scaleUpAnimation);
        //parentAnimation.Add(0, 1, rotateAnimation);
        //parentAnimation.Add(0.5, 1, scaleDownAnimation);
        //parentAnimation.Commit(this, "ChildAnimations", 16, 4000, null, (v, c) => { }, () => true);


        var animations = new Animation
        {
            { 0, 0.5, new Animation (v => {
                image.Scale = v;
                //System.Diagnostics.Debug.WriteLine($"+++++ V: {v} ++++++ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} ++++++++++++++++++++++++");
            }, 1, 2) },
            { 0.5, 1, new Animation (v => image.Scale = v, 2, 1) },
            { 0, 1, new Animation (v =>
            {
              image.Rotation = v;
              System.Diagnostics.Debug.WriteLine($"+++++ V: {v} ++++++ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} ++++++++++++++++++++++++");
            }
            , 0, 360) },
        };
        animations.Commit(this, "ChildAnimations", 16, 4000, null, (v, c) =>
        {
            System.Diagnostics.Debug.WriteLine($"----- V: {v} ------ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} ------------------------");
        }, () => true);


        //await Task.WhenAll
        //           (
        //             image.TranslateTo(200, 200, 1000, Easing.CubicOut),
        //             image.FadeTo(0, 500)
        //           );
        //await image.FadeTo(1);
        //await image.TranslateTo(0, 0);
    }

    private void Button_Clicked_23(object sender, EventArgs e)
    {
        new Animation(callback: v =>
        {
            BackgroundColor = Color.FromHsla(v, 1, 0.5);
            System.Diagnostics.Debug.WriteLine($"---- V: {v} -------{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}------------");
        },
        start: 0,
        end: 2).Commit(this, "Animation", 16, 4000, Easing.CubicInOut, (v, c) => BackgroundColor = Colors.White);
    }
}